package com.cy.sys.serviceImpl;

import com.cy.common.annotation.RequiredCache;
import com.cy.common.excetion.ServiceException;
import com.cy.sys.dao.RoleDao;
import com.cy.sys.pojo.PageObject;
import com.cy.sys.pojo.Role;
import com.cy.sys.pojo.Rolemenu;
import com.cy.sys.pojo.checkbox;
import com.cy.sys.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao role;
    @Override

    public PageObject doFindPageObjects(String username, Long pageCurrent ) {
        if(pageCurrent==null||pageCurrent<0)
            throw new IllegalArgumentException("页码值不正确");
        Long rowCount=role.RowCount(username);
        if(rowCount==0)
            throw new ServiceException("查无此人");
        int pageSize=5;
        long startIndex=(pageCurrent-1)*pageSize;

        List<Role> records=role.doFindPageObjects(username,startIndex,pageSize);
        return new PageObject(pageCurrent,pageSize,rowCount,records);
    }

    @Override
    public int doDeleteObject(Integer id) {
        role.doDeleteObject(id);
        role.doDeleteOther(id);
        //判断删除返回值数据

        return 0;
    }

    @Override
    public Rolemenu doFindObjectById(Integer id) {
        Rolemenu list= role.doFindObjectById(id);


        return list;
    }

    @Override
    public int doUpdateObject(Rolemenu rolemenu) {
        Integer id=rolemenu.getId();
        String name=rolemenu.getName();
        String note=rolemenu.getNote();
        int a=role.doUpdateObject(id,name,note);
        if(a==0)
            throw new IllegalArgumentException("修改失败");
        List<Integer> menuIds=rolemenu.getMenuIds();
        role.doDeleteOther(id);
        for (Integer menuId : menuIds) {
            role.insertqx(menuId,id);
        }



        return 0;
    }

    @Override
    public int doSaveObject(Rolemenu rolemenu) {

        String name=rolemenu.getName();
        String note=rolemenu.getNote();
        List<Integer> menuIds=rolemenu.getMenuIds();
        role.doSaveObject(name,note);
        int id=role.doFindId(name);
        System.out.println(id);
        for (Integer menuId : menuIds) {
            role.insertqx(menuId,id);
        }
            //判断
        return 0;
    }

    @Override
    public List<checkbox> doFindRoles(Integer id) {
        List<checkbox> roleIds= role.doFindRoles(id);
        System.out.println(roleIds);
        return roleIds;
    }
}
